package com.abbas.myviewtest.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.myviewtest.R;
import com.abbas.myviewtest.entity.Record;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/3/31 23:55
 */
public class ScoreListAdapter extends BaseAdapter {

    private List<Record> records;
    private Context mContext;

    public ScoreListAdapter(List<Record> records, Context mContext) {
        this.records = records;
        this.mContext = mContext;
    }

    public void setRecords(List<Record> records) {
        this.records = records;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int position) {
        return records.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.score_item,null,false);
            viewHolder.avater = (ImageView) convertView.findViewById(R.id.avater);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tvFactorial = (TextView) convertView.findViewById(R.id.tv_factorial);
            viewHolder.tvUsingTime = (TextView) convertView.findViewById(R.id.tv_usingTime);
            viewHolder.tvStep = (TextView) convertView.findViewById(R.id.tv_step);
            viewHolder.createTime = (TextView) convertView.findViewById(R.id.createTime);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Record record = records.get(position);
        if(record.getAvater() == null || TextUtils.isEmpty(record.getAvater())){
            Glide.with(mContext)
                    .load(R.drawable.ic_launcher_background)
                    .transform(new RoundedCorners(8))
                    .into(viewHolder.avater);
        }else {
            Glide.with(mContext)
                    .load(record.getAvater())
                    .transform(new RoundedCorners(8))
                    .into(viewHolder.avater);
        }
        viewHolder.tvName.setText(record.getName()+"");
        viewHolder.createTime.setText(record.getCreateTime());
        viewHolder.tvFactorial.setText(record.getFactorial()+"阶");
        switch (record.getFactorial()){
            case 2:
                viewHolder.tvFactorial.setBackground(mContext.getResources().getDrawable(R.drawable.shape_factorial_text_green));
                break;
            case 3:
                viewHolder.tvFactorial.setBackground(mContext.getResources().getDrawable(R.drawable.shape_factorial_text_blue));
                break;
            case 4:
                viewHolder.tvFactorial.setBackground(mContext.getResources().getDrawable(R.drawable.shape_factorial_text_purple));
                break;
            case 5:
                viewHolder.tvFactorial.setBackground(mContext.getResources().getDrawable(R.drawable.shape_factorial_text_orange));
                break;
            case 6:
                viewHolder.tvFactorial.setBackground(mContext.getResources().getDrawable(R.drawable.shape_factorial_text_red));
                break;
            default:
                viewHolder.tvFactorial.setBackground(mContext.getResources().getDrawable(R.drawable.shape_factorial_text_red));
                break;
        }
        viewHolder.tvStep.setText("使用步数："+record.getStep()+"步");
        viewHolder.tvUsingTime.setText("游戏时间："+record.getUsingTime()+"分钟");

        return convertView;
    }

    static class ViewHolder{
        ImageView avater;
        TextView tvName;
        TextView tvFactorial;
        TextView tvUsingTime;
        TextView tvStep;
        TextView createTime;
    }
}
